package com.androidnetframe;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

import com.androidnetframe.exception.CodeTypeObject;
import com.androidnetframe.exception.MypreDrawListener;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.utils.CustomToast;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;

public class BaseParentActivity extends FragmentActivity {

    public MypreDrawListener mypreDrawListener;
    public HashMap<String, OverideParams> filterRootView_errorView = new HashMap<String, OverideParams>();

/*内部使用========================================================================================*/

/*内部使用========================================================================================*/
	
/*外部使用=============================================================================================*/

    /*内部类=============================================================================================*/
    public class OverideParams {
        public BaseRequestFilterLayer filter;
        public ViewGroup rootView;
        private View errorView;
        public boolean errorState = false;//true:错误页面显示 false：错误页面没有显示
        public View loadingView;
        public boolean loadingViewState = false;//true:错误页面显示 false：错误页面没有显示

        public void removeErrorView() {
            if (errorView != null) {
                rootView.removeView(errorView);
                errorState = false;
            }
        }

        public void addErrorView(View errorView) {
            this.errorView = errorView;
            if (errorView != null) {
                if (errorView.getParent() != null) {
                    rootView.removeView(errorView);
                }
                rootView.addView(errorView);
                errorState = true;
            }
        }

        public void removeLoadingView() {
            if (loadingView != null) {
                rootView.removeView(loadingView);
                loadingViewState = false;
            }
        }

        public void addLoadingView(View loadingView) {
            this.loadingView = loadingView;
            if (loadingView != null) {
                if (loadingView.getParent() != null) {
                    rootView.removeView(loadingView);
                }
                rootView.addView(loadingView);
                loadingViewState = true;
            }
        }
    }
	/*内部类=============================================================================================*/
	
	/*方法=============================================================================================*/

    /**
     * 添加覆盖页面
     */
    public void addOverideLayout(final ViewGroup rootView, final boolean isTransparence, final CodeTypeObject codeTypeObject, final BaseRequestFilterLayer filter) {
		/*展示错误布局*/
        if (rootView == null) {
            return;
        }
        if (rootView.getMeasuredHeight() == 0) {//如果是首次加载
            mypreDrawListener = new MypreDrawListener(rootView, isTransparence, codeTypeObject, filter, this);
            rootView.getViewTreeObserver().addOnPreDrawListener(mypreDrawListener);
        } else {//如果是非首次加载
            if (mypreDrawListener == null) {
                mypreDrawListener = new MypreDrawListener(rootView, isTransparence, codeTypeObject, filter, this);
            }
            mypreDrawListener.init(rootView, isTransparence, codeTypeObject, filter, this);
            mypreDrawListener.onPreDraw();//非首次加载
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    /**
     * 展示Toast
     */
    public void showToast(Context context, View view, int millisecond) {
        CustomToast customToast = new CustomToast(context);
        customToast.setView(view);
        customToast.setDuration(millisecond);
        //设置WindowManger能够显示的区域
        customToast.setGravity(Gravity.FILL_HORIZONTAL, 0, 0);
        customToast.show();
    }

    /**
     * 移除Loadinig框
     */
    public void removeLoadingLayout(BaseRequestFilterLayer filter) {
        OverideParams errorParams = this.filterRootView_errorView.get(filter.hashCode()+filter.getClass().getName());
        //移除loadding框
        if (errorParams != null) {
            if (errorParams.loadingViewState) {
                errorParams.removeLoadingView();
            }
        }
    }

    /**
     * 移除Loadinig框
     */
    public void removeErrorLayout(BaseRequestFilterLayer filter) {
        OverideParams errorParams = this.filterRootView_errorView.get(filter.hashCode()+filter.getClass().getName());
        if (errorParams != null) {
            if (errorParams.errorState) {
                errorParams.removeErrorView();
            }
        }
    }
	/*方法=============================================================================================*/
/*外部使用=============================================================================================*/

}
